/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.user.service.impl;

import java.util.Objects;
import java.util.Optional;

import com.hyts.infodb.user.util.PasswordHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hyts.infodb.enums.StatusTypeEnum;
import com.hyts.infodb.error.subclass.DaoException;
import com.hyts.infodb.user.bean.bo.UserBO;
import com.hyts.infodb.user.bean.po.UserPO;
import com.hyts.infodb.user.bean.vo.UserVO;
import com.hyts.infodb.user.dao.UserDAO;
import com.hyts.infodb.user.service.UserService;

/** 
 * @author LiBo/Alex
 * @see com.hyts.infodb.user.service.UserService
 * @version V1.0 
 */
@Service
public class UserServiceImpl implements UserService {

    
    @Autowired(required = false)
    private UserDAO userDao;
    
    @Autowired
    private PasswordHelper passwordHelper;


    @Override
    public UserVO findUserList(UserBO user) {
        UserVO vo = new UserVO();
        return (UserVO) vo.setPageSubject(userDao.findAll(user.buildPageData(user.getPageNum(), user.getPageSize())));
    }

    /**
     * 加载用户信息
     * @param user
     * @return
     */
    @Override
    public UserVO loadUser(UserBO user) {
        UserVO vo = new UserVO();
        UserPO userPO = userDao.findById(user.getId()).get();
        if(Objects.nonNull(userPO)) {
            vo.setContent(userDao.findById(user.getId()).get());
        }
        return vo;
    }

    /**
     * 保存用户信息
     * @param user
     * @return
     */
    @Override
    public boolean saveUser(UserBO user) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(user, userPO);
        if(userPO.getSalt() == null) {
            userPO.setSalt("infodb");
        }
        userPO.setEncryptPassword(passwordHelper.encryptPasswordStr(userPO.getSalt(),userPO.getPassword()));
        if(userPO.getState() == null) {
            userPO.setState(StatusTypeEnum.UN_VALIDATE.getValue().byteValue());
        }
        return Optional.ofNullable(userDao.save(userPO)).isPresent();
    }

    @Override
    public boolean deleteUser(UserBO user) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(user, userPO);
        try {
            userDao.delete(userPO);
        } catch (Exception e) {
            throw new DaoException("删除用户失败!",e);
        }
        return true;
    }


	@Override
	public UserVO getUser(String userName) {
		UserVO userVO = new UserVO();
		UserPO userPO = userDao.findByUserName(userName);
		userVO.setContent(userPO);
		return userVO;
	}

}
